package com.github.kassadin.kotlinandroid

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import org.jetbrains.anko.find

/**
 *
 * Created by <b>kassadin@foxmail.com</b> on 16/1/22 15:16
 */
class ForecastListAdapter(val context: Context, var retData: RetData?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    val TYPE_TODAY = 1
    val TYPE_INDEX = 2
    val TYPE_FORECAST = 3

    val inflater: LayoutInflater = LayoutInflater.from(context);

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {

        return when (viewType) {
            TYPE_TODAY -> TodayViewHolder(inflater.inflate(R.layout.rv_view_today, parent, false))
            TYPE_INDEX -> IndexViewHolder(inflater.inflate(R.layout.rv_view_index, parent, false))
            TYPE_FORECAST -> ForecastViewHolder(inflater.inflate(R.layout.rv_view_forecast, parent, false))
            else -> throw IllegalStateException("xx")
        }

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val viewType = getItemViewType(position)
        when (holder) {
            is TodayViewHolder -> {
                val today = retData!!.today
                holder.city.text = retData?.city
                holder.curTemp.text = today.curTemp
                holder.date.text = today.date.split("-").filterIndexed { i, s -> i != 0 }.joinToString("/")
                holder.week.text = today.week.replace("星期", "周")
                holder.type.text = today.type
                holder.temp.text = today.hightemp + "~" + today.lowtemp
            }

            is IndexViewHolder -> {
                var index: Index = retData!!.today.index[position - 1]
                holder.details.text = index.details
                holder.name.text = index.name
                holder.index.text = index.index
            }

            is ForecastViewHolder -> {
                var forecast: Forecast = retData!!.forecast[position - 1 - retData!!.getIndexCount()]
                holder.week.text = forecast.week.replace("星期", "周")
                holder.temp.text = forecast.hightemp + "~" + forecast.lowtemp
                holder.type.text = forecast.type
                holder.feng.text = forecast.fengxiang + " " + forecast.fengli
            }
        }
    }


    override fun getItemCount(): Int {
        return retData?.getItemCount() ?: 0
    }


    override fun getItemViewType(position: Int): Int {
        return if (position == 0) TYPE_TODAY
        else if (position <= retData?.getIndexCount() ?: 0) TYPE_INDEX
        else TYPE_FORECAST
    }

    class ForecastViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var week: TextView = itemView.find(R.id.week)
        var temp: TextView = itemView.find(R.id.temp)
        var type: TextView = itemView.find(R.id.type)
        var feng: TextView = itemView.find(R.id.feng)
    }

    class IndexViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var name: TextView = itemView.find(R.id.name)
        var index: TextView = itemView.find(R.id.index)
        var details: TextView = itemView.find(R.id.details)
    }

    class TodayViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var city: TextView = itemView.find(R.id.city)
        var date: TextView = itemView.find(R.id.date)
        var week: TextView = itemView.find(R.id.week)
        var curTemp: TextView = itemView.find(R.id.curTemp)
        var type: TextView = itemView.find(R.id.type)
        var temp: TextView = itemView.find(R.id.temp)
    }
}